/*
 * clients.c
 *
 *  Created on: 2012. 4. 12.
 *      Author: jphong
 */
#include	<string.h>
#include	<stdlib.h>
#include	"ft_info.h"

#define MAXLINE	4096
#define	MAXID	128

static ft_info *ft_recvList = NULL;
static ft_info *ft_recvfreeList = NULL;

static ft_info *ft_sendList = NULL;
static ft_info *ft_sendfreeList = NULL;

ft_info *
getFTList(int flag) {
	if (flag == 1)
		return (ft_recvList);
	if (flag == 2)
		return (ft_sendList);
	else {
		return (NULL);
	}
}

/* Create a client and insert it to clientList
 * Returns: pointer to client node
 *          NULL if couldn't create (Connection should be closed and rejected)
 */
ft_info *
createFT_info(int id, int flag) { //char* address, char* filename, char* my_id, char* comp_id){
	ft_info *c;

	if (flag == 1) {
		if (ft_recvfreeList == NULL) {
			// If freeList is empty, allocate memory for a client.
			if (((c = (ft_info *) malloc(sizeof(ft_info))) != NULL)
					&& ((c->component_id = (char *) malloc((size_t) MAXID))
							!= NULL)
					&& ((c->my_id = (char *) malloc((size_t) MAXID)) != NULL)
					&& ((c->address = (char *) malloc((size_t) 20)) != NULL)
					&& ((c->filename = (char *) malloc((size_t) MAXLINE))
							!= NULL)) {
				c->next = ft_recvfreeList;
				ft_recvfreeList = c;
			} else
				return (NULL); // Not enough memory. return NULL
		}

		// Get a client node from freeList
		c = ft_recvfreeList;
		ft_recvfreeList = ft_recvfreeList->next;


		// Initialize the client node
		c->ft_id = id;
		c->state=0;
		*(c->component_id) = '\0';
		*(c->my_id) = '\0';
		*(c->address) = '\0';
		*(c->filename) = '\0';

		// Insert the client node in front of clientList
		c->next = ft_recvList;
		ft_recvList = c;
	} else if (flag == 2) {

		if (ft_sendfreeList == NULL) {
			// If freeList is empty, allocate memory for a client.
			if (((c = (ft_info *) malloc(sizeof(ft_info))) != NULL)
					&& ((c->component_id = (char *) malloc((size_t) MAXID))
							!= NULL)
					&& ((c->my_id = (char *) malloc((size_t) MAXID)) != NULL)
					&& ((c->address = (char *) malloc((size_t) 20)) != NULL)
					&& ((c->filename = (char *) malloc((size_t) MAXLINE))
							!= NULL)) {
				c->next = ft_sendfreeList;
				ft_sendfreeList = c;
			} else
				return (NULL); // Not enough memory. return NULL
		}

		// Get a client node from freeList
		c = ft_sendfreeList;
		ft_sendfreeList = ft_sendfreeList->next;

		// Initialize the client node
		c->ft_id = id;
		*(c->component_id) = '\0';
		*(c->my_id) = '\0';
		*(c->address) = '\0';
		*(c->filename) = '\0';

		// Insert the client node in front of clientList
		c->next = ft_sendList;
		ft_sendList = c;

	}

	return (c); // returns pointer to the client node
}

/* Destroy the client
 * 		Note: allocated memory will be reused.
 */

ft_info* findFT_info(int id, int flag) {

	ft_info *ft;

	if (flag == 1){
		ft = ft_recvList;
}
	else if (flag == 2){
		ft = ft_sendList;

	}
	for (; ft != NULL; ft = ft->next) {

		if (ft->ft_id == id){
			return (ft);
		}
	}

	return (NULL);

}

void destroyFT_info(ft_info *c, int flag) {
	ft_info *cur, **pre;
	if(flag == 1){

		// remove the client from clientList
		for (pre = &ft_recvList, cur = ft_recvList; (cur != c); pre = &(cur->next), cur =cur->next); // find predecessor
		*pre = c->next; //remove the client

		// insert it to the front of freeList
		c->next = ft_recvfreeList;
		ft_recvfreeList = c;

	}

	else if(flag == 2){

		// remove the client from clientList
		for (pre = &ft_sendList, cur = ft_sendList; (cur != c); pre = &(cur->next), cur =cur->next); // find predecessor
		*pre = c->next; //remove the client

		// insert it to the front of freeList
		c->next = ft_sendfreeList;
		ft_sendfreeList = c;

	}


}
